package com.kittlen.managementSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kittlen.managementSystem.entity.Choice;
import com.kittlen.managementSystem.entity.Coach;
import com.kittlen.managementSystem.entity.Student;
import com.kittlen.managementSystem.mapper.ChoiceMapper;
import com.kittlen.managementSystem.service.IChoiceService;
import com.kittlen.managementSystem.service.ICoachService;
import com.kittlen.managementSystem.service.IStudentService;
import com.kittlen.managementSystem.utils.ObjectUtil;
import com.kittlen.managementSystem.utils.TimeUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 学员选择表 服务实现类
 * </p>
 *
 * @author kittlen
 * @since 2021-01-26
 */
@Service
public class ChoiceServiceImpl extends ServiceImpl<ChoiceMapper, Choice> implements IChoiceService {
    Logger logger=Logger.getLogger(this.getClass().getName());

    @Resource
    IStudentService iStudentService;

    @Resource
    ICoachService iCoachService;

    @Override
    public List<Choice> selects(Wrapper wrapper) {
        return baseMapper.selects(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updates(Choice.Choices choices,int studyType,String coachId)throws Exception {
        try {
            if(choices.getChoiceIds().size()>0) {
                UpdateWrapper<Choice> updateWrapper = new UpdateWrapper<>();
                updateWrapper.in("choice_id", choices.getChoiceIds()).set("stage_end", studyType);
                if(1==studyType){
                    updateWrapper.set("end_date", TimeUtil.getNowDate());
                }
                this.update(updateWrapper);
            }
            if(choices.getStudentIds().size()>0) {
                iStudentService.update(new UpdateWrapper<Student>().in("student_id", choices.getStudentIds()).set("study_type", studyType));
                if(1==studyType){
                    if(ObjectUtil.isNoEmpty(coachId)) {
                        Coach coach = iCoachService.getById(coachId);
                        if(coach!=null){
                            coach.setChoiceCount(coach.getChoiceCount()-1>=0?coach.getChoiceCount()-1:0);
                        }
                        iCoachService.update(new UpdateWrapper<Coach>().set("choice_count",coach.getChoiceCount()).eq("coach_id",coachId));
                    }
                }
            }
            return true;
        }catch (Exception e){
            logger.error("修改学员学习阶段错误:参数为"+choices.toString());
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(Choice choice) throws Exception {
            //还有学员空位&&未离职&&能被选取&&学员当前阶段与教练教学阶段匹配
            if (choice.getCoach().getChoiceCountMax() > choice.getCoach().getChoiceCount() && !choice.getCoach().getIsEnd() && choice.getCoach().getIsStartChoice()&&choice.getStudent().getStudyStatus()==choice.getCoach().getTeachingStage()) {
                try {
                    if (baseMapper.insert(choice) > 0) {
                        //修改教练的当前学员人数
                        iCoachService.update(new UpdateWrapper<Coach>().eq("coach_id", choice.getCoach().getCoachId()).set("choice_count", choice.getCoach().getChoiceCount() + 1));
                        //修改学员的当前阶段状态为学习中
                        iStudentService.update(new UpdateWrapper<Student>().eq("student_id",choice.getStudent().getStudentId()).set("study_type",0));
                        return true;
                    }
                }catch (Exception e){
                    throw e;
                }
            }
        return false;
    }
}
